package com.laipai.galaryManInfo.app;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.laipai.app.annotation.NotLogin;
import com.laipai.app.common.JSONConvertUtil;
import com.laipai.app.common.JSONTools;
import com.laipai.app.process.app.RequestExecutor;
import com.laipai.app.process.app.dataobj.BaseRequestParameters;
import com.laipai.base.dao.IBaseDao;
import com.laipai.base.util.LaipaiConstants;
import com.laipai.galaryManInfo.bean.NewIntroducedBean;
import com.laipai.galaryManInfo.dao.IGalleryDao;
import com.laipai.galaryManInfo.dto.LpHeadShowGalaryBean;
import com.laipai.galaryManInfo.dto.SimpleImage;
import com.laipai.galaryManInfo.pojo.LpGalary;
import com.laipai.galaryManInfo.pojo.LpNewintroduce;
import com.laipai.galaryManInfo.pojo.VLpGalary;
import com.laipai.galaryManInfo.pojo.VLpGalaryAppShow;
import com.laipai.galaryManInfo.pojo.VLpGalaryBackinfo;
import com.laipai.logs.pojo.LpGalaryLog;
import com.laipai.logs.pojo.LpGalaryShowlog;
import com.laipai.operationManage.pojo.LpCity;
import com.laipai.operationManage.pojo.LpStyle;
import com.laipai.serviceInfo.pojo.LpServiceDetail;
import com.laipai.serviceInfo.pojo.LpServiceStyle;
import com.laipai.subject.pojo.Subject;
import com.laipai.userManInfo.action.UserManageAction;
import com.laipai.userManInfo.dto.LpUserBean;
import com.laipai.userManInfo.pojo.LpUser;

/**
 * 首页作品集展示
 * 
 * @author zhh
 */
@NotLogin
@Service("GalleryShowExecutorImpl")
public class GalleryShowExecutorImpl implements RequestExecutor {
	
	private static Logger logger = Logger.getLogger(GalleryShowExecutorImpl.class);
	@Autowired
	private IBaseDao baseDao;
	@Resource(name=IGalleryDao.DAO_NAME)
	private IGalleryDao galleryDao;
	
	/**
	 * 重写http调用方法
	 * */
	public Object execute(BaseRequestParameters parameters, Object... arg) {
		long startTime = System.currentTimeMillis();
		JSONObject json = parameters.getJson();
		JSONObject baseJson = new JSONObject();
		//登录用户的id
		String userId = json.getString("userId");
		LpUser user  = null;
		//获取服务器域名
		String serverName = json.getString("serverName");
		String cityId = json.getString("cityId");
		String styleId = json.getString("styleId");
		String isSearch=JSONTools.getString(json,"isSearch");
		String page = json.getString("page");
		String machineId=JSONTools.getString(json,"imei");
		/*//基础查询sql
		String hql = "select "
					+" distinct lg.galary_id AS galaryId, "
					+" lg.subject_name AS subjectName, "
					+" lg.galary_cover AS galaryCover, "
					+" if((lg.control_source = 0),lg.view_number,lge.view_number) AS viewNumber, "
					+" if((lg.control_source = 0),lg.like_number,lge.like_number) AS likeNumber, "
					+" if((lg.control_source = 0),lg.comment_number,lge.comment_number) AS commentNumber, "
					+" lu.user_name AS userName, "
					+" lu.nick_name AS nickName, "
					+" lu.user_id AS userId, "
					+" lu.head_image AS headImage, "
					+" lu.user_type AS userType, "
					+" lc.city_name AS cityName, "
					+" ld.price AS price "
					+" from lp_galary lg "
					+" left join lp_galary_extend lge on lge.galary_id = lg.galary_id  "
					+" left join lp_user lu on lu.user_id = lg.user_id  "
					+" left join lp_city lc on lc.city_id = lu.city_id  "
					+" left join lp_service_detail ld on ld.service_id = lg.service_id "
					+" left join lp_service_style lss on lss.detail_id = ld.detail_id  "
					+" where 1=1 "
					+" and lg.galary_status=0 "
					+" and lg.status=0 ";
		//加入城市筛选
		if(StringUtils.isNotEmpty(cityId)){
			cityId = cityId.replace(",", "','");
			cityId = "'" + cityId + "'";
			hql += " and lc.city_id in ("+cityId+") ";
		}
		//加入风格筛选
		if(StringUtils.isNotEmpty(styleId)){
			styleId = styleId.replace(",", "','");
			styleId = "'" + styleId + "'";
			hql += " and lss.style_id in ("+styleId+") ";
		}
		//加入动态加载(每页显示10条)
//		int v_hiRownum = p_pageNo * p_pageSize;
//		int v_loRownum = v_hiRownum - p_pageSize + 1;
//		hql = " select * FROM (  select A.*, rownum rn From (" + hql+ ") A   where rownum <= "+ v_hiRownum + " ) B  where rn >= " + v_loRownum;
		
		logger.info("----查询首页作品集sql: "+hql);
		List pojolist = baseDao.querySqlObject(hql);
		//LpHeadShowGalaryBean中全是基本变量，可以转json
		List<LpHeadShowGalaryBean> beanList = new ArrayList<LpHeadShowGalaryBean>();
		Map lpGalary = null;
		for(int i=0;i<pojolist.size();i++){
			lpGalary = (Map) pojolist.get(i);
			LpHeadShowGalaryBean  lpGalaryBean = new LpHeadShowGalaryBean();
			lpGalaryBean.setGalaryId(lpGalary.get("galaryId")+"");
			lpGalaryBean.setGalaryCover(lpGalary.get("galaryCover")+"");
			lpGalaryBean.setSubjectName(lpGalary.get("subjectName")+"");
			lpGalaryBean.setUserId(lpGalary.get("userId")+"");
			lpGalaryBean.setViewNumber(Integer.parseInt(StringUtils.isEmpty(lpGalary.get("viewNumber")+"") || "null".equals(lpGalary.get("viewNumber")+"")?"0":(lpGalary.get("viewNumber")+"")));
			lpGalaryBean.setLikeNumber(Integer.parseInt(StringUtils.isEmpty(lpGalary.get("likeNumber")+"") || "null".equals(lpGalary.get("likeNumber")+"")?"0":(lpGalary.get("likeNumber")+"")));
			lpGalaryBean.setCommentNumber(Integer.parseInt(StringUtils.isEmpty(lpGalary.get("commentNumber")+"") || "null".equals(lpGalary.get("commentNumber")+"")?"0":(lpGalary.get("commentNumber")+"")));
//			BeanUtils.copyProperties(lpGalary, lpGalaryBean);
			//城市
			lpGalaryBean.setCityName((String) lpGalary.get("cityName"));
			//风格
			String sql = " select distinct lst.style_name from lp_style lst "
					+" left join lp_service_style lss on lss.style_id = lst.style_id "
					+" left join lp_service_detail lsd on lsd.detail_id = lss.detail_id "
					+" left join lp_galary lg on lg.service_id = lsd.service_id "
					+" where lg.galary_id = '"+lpGalary.get("galaryId")+"'";
			List styleList = baseDao.querySqlObject(sql);
			if(styleList!=null){
				if(styleList.size()>0){
					Map map = (Map) styleList.get(0);
					lpGalaryBean.setStyleName(map.get("style_name")+"");
				}
			}
			//是否点过赞
			try {
				String likeSql = "from LpLike where lpGalary.galaryId='"+lpGalary.get("galaryId")+"' and lpUser.userId='"+userId+"' and likeStatus=0";
				int likecheck = baseDao.getCount(likeSql);
				if(likecheck == 0){
					lpGalaryBean.setIsLike(1);  //没有赞过
				}else{
					lpGalaryBean.setIsLike(0);  //赞过
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			//价格
			lpGalaryBean.setPrice(Integer.parseInt(StringUtils.isEmpty(lpGalary.get("price")+"") || "null".equals(lpGalary.get("price")+"")?"0":(lpGalary.get("price")+"")));
			//摄影师登录账号
			lpGalaryBean.setUserName((String) lpGalary.get("userName"));
			//摄影师昵称
			lpGalaryBean.setNickName(StringUtils.isBlank(lpGalary.get("nickName")+"")?"":(lpGalary.get("nickName")+""));
			//摄影师头像
			if(StringUtils.isNotBlank(lpGalary.get("headImage")+"")){
				lpGalaryBean.setHeadImage(serverName + (lpGalary.get("headImage")+"").replace(LaipaiConstants.UPLOAD_ABSOLUTE_IMG+LpUserBean.LP_USER_IMGURL, LaipaiConstants.UPLOAD_VIRTUAL_IMG+LpUserBean.LP_USER_IMGURL));
			}
			//修改图片路径,加上服务器域名,并把实际路径替换为虚拟路径
			lpGalaryBean.setGalaryCover(serverName + lpGalaryBean.getGalaryCover().replace(LaipaiConstants.UPLOAD_ABSOLUTE_IMG+SimpleImage.LP_GALLERY_IMGURL, LaipaiConstants.UPLOAD_VIRTUAL_IMG+SimpleImage.LP_GALLERY_IMGURL));
			beanList.add(lpGalaryBean);
		}*/
		List<VLpGalaryAppShow> galaryList = getAllBypage(serverName,userId,cityId,styleId,page);
		 //如果不是搜索
		if(!"TRUE".equalsIgnoreCase(isSearch)){
			
			if(galaryList!=null&&galaryList.size()>0){
			this.insertSubject(galaryList, serverName,page);
			}
			
		}
		
		if(StringUtils.isNotEmpty(styleId)){
			 LpStyle style= (LpStyle) baseDao.getObjectById(LpStyle.class, styleId);
			 String styleName="";
			 if(style!=null){				 
				 styleName=style.getStyleName();
			 }
			 if(galaryList!=null&&galaryList.size()>0){
				  for(int i=0;i<galaryList.size();i++){
					  VLpGalaryAppShow  galary=  galaryList.get(i);
					  galary.setStyleName(styleName);
					 
				  }
				 
			 }
			 
			 
		 }
		 
		 if(galaryList!=null&&galaryList.size()>0){
			  for(int i=0;i<galaryList.size();i++){
				  VLpGalaryAppShow  galary=  galaryList.get(i);
				  String galaryId=galary.getGalaryId();
				  if(StringUtils.isNotBlank(galaryId)){
				  int countNumber= this.getCommentCount(galaryId);
				  galary.setCommentNumber(countNumber);
				  inserLog(galary.getGalaryId(), userId, machineId);
				  }
			  }
			 
		 } 
		 
		//作品集
		baseJson.put("lpGalary", JSONConvertUtil.allObjectToJSON(galaryList));
		//新晋摄影师介绍
		List<LpNewintroduce> list = baseDao.queryListObjectByTopNum("from LpNewintroduce where status=1", 1); 
		if(list !=null && !list.isEmpty()){
			LpNewintroduce introduce = list.get(0);
			NewIntroducedBean inroduceBean = new NewIntroducedBean();
			BeanUtils.copyProperties(introduce, inroduceBean);
			//修改图片路径,加上服务器域名,并把实际路径替换为虚拟路径
			inroduceBean.setImgUrl(serverName + inroduceBean.getImgUrl().replace(LaipaiConstants.UPLOAD_ABSOLUTE_IMG, LaipaiConstants.UPLOAD_VIRTUAL_IMG));
			//新建摄影师介绍
			baseJson.put("newIntroduce", JSONObject.fromObject(inroduceBean));
		}
		//用户类型 0普通用户  1摄影师 (因普通用户和摄影师的显示界面不一样)
		if(StringUtils.isNotBlank(userId)){//已登录
			System.out.println("----------------首页传进来userId="+userId);
			user = (LpUser) baseDao.getObjectById(LpUser.class, userId);
			if(user !=null){//用户id不存在，则以普通用户身份登录
				baseJson.put("userType", user.getUserType());  
				//消息数量(messageNumber)
				try {
					int messageNumber = baseDao.getCount("from LpMessageDetail where messageStatus=0 and recieveUserId='"+userId+"'");
					baseJson.put("messageNumber", messageNumber);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}else{
				baseJson.put("userType", 0);
				baseJson.put("messageNumber", 0);
			}
		}else{//未登录
			baseJson.put("userType", 0);
			baseJson.put("messageNumber", 0);
		}
		//查询城市
		String citySql =" SELECT "
						+" lc.city_id as cityId, "
						+" lc.city_name as cityName, "
						+" lc.city_location as cityLocation "
						+" FROM "
						+" lp_city lc "
						+" where 1=1 "
						+" and lc.city_status=1 ";
		if(StringUtils.isNotEmpty(userId) && user !=null){
			citySql += "union "
					+" SELECT "
					+" lc.city_id as cityId, "
					+" lc.city_name as cityName, "
					+" lc.city_location as cityLocation "
					+" FROM "
					+" lp_city lc "
					+" left join lp_user lu on lu.city_id=lc.city_id "
					+" where 1=1 "
					+" and lu.user_id='"+userId+"' ";
		}
		citySql = " select a.cityId,a.cityName from ("+citySql+") a order by a.cityLocation ";
		List cityList = baseDao.querySqlObject(citySql);
		baseJson.put("city", JSONConvertUtil.allObjectToJSON(cityList));
		//查询风格
		List styleList = baseDao.querySqlObject("select style_id styleId,style_name styleName from lp_style where is_online=1");
		baseJson.put("style", JSONConvertUtil.allObjectToJSON(styleList));
		baseJson.put("page", page); //app返回的页数
		long endTime = System.currentTimeMillis();
		System.out.println("############################################");
		System.out.println("##########################首页后台处理数据耗时："+(endTime-startTime)+"ms");
		System.out.println("############################################");
		return baseJson;
	}
	
	/**
	 * 查询作品集
	 * 使用视图v_lp_galary_app_show查询
	 * 按分页方式查询,并区分先后顺序
	 * */
	public List<VLpGalaryAppShow> getAllBypage(String serverName,String userId,String cityId,String styleId,String pageStr) {
		String serviceId = "";
		String hql = "from VLpGalaryAppShow where 1=1";
		if(StringUtils.isNotEmpty(cityId)){
			cityId = cityId.replace(",", "','");
			cityId = "'" + cityId + "'";
			hql += " and cityId in (" +cityId+")";
		}
		if(StringUtils.isNotEmpty(styleId)){
			styleId = styleId.replace(",", "','");
			styleId = "'" + styleId + "'";
			String serviceSql = "SELECT "
							  +" lp_service_detail.service_id serviceId"
							  +" FROM "
							  +" lp_service_detail "
							  +" left join lp_service_style on lp_service_detail.detail_id = lp_service_style.detail_id "
							  +" left join lp_style on lp_style.style_id = lp_service_style.style_id "
							  +" where 1=1 "
							  +" and lp_style.style_id in ("+styleId+") "
							  +" and lp_style.is_true_delete !=0 "
							  +" and lp_style.is_online= 1";
			List list = baseDao.querySqlObject(serviceSql);
			StringBuffer sId = new StringBuffer("");
			for(int i=0;i<list.size();i++){
				Map map = (Map) list.get(i);
				if(i !=0){
					sId.append(",");
				}
				sId.append(map.get("serviceId"));
			}
			System.out.println("&&&&&&&&&&&&&&&&&&&&&&sId="+sId);
			serviceId = sId.toString().replace(",", "','");
			serviceId = "'" +serviceId +"'";
			hql += " and serviceId in ("+serviceId+")";
		}
		hql +=" order by galaryScores DESC";
		List<VLpGalaryAppShow> controllist=null;
		int page = 1;
		try {
			List<VLpGalaryAppShow> list = null;
			if(StringUtils.isEmpty(pageStr) || "null".equals(pageStr)){
				list = this.baseDao.queryListObjectAll(hql);
			}else{
				page = Integer.parseInt(pageStr);
				list = this.baseDao.queryListObjectAllForPage(hql, 10, page);
			}
			//该页的起始位置
			int begin =(page-1)*10+1;
			//该页的结束位置
			int end=page*10;
			String controlHql="from VLpGalaryAppShow where indexControl=1 ";
			if(StringUtils.isNotEmpty(cityId)){
				controlHql += " and cityId in (" +cityId+") ";
			}
			if(StringUtils.isNotEmpty(serviceId)){
				controlHql += " and serviceId in ("+serviceId+") ";
			}
			controlHql += " and galaryIndex between " + begin + " and " + end + " order by galaryIndex ";
			controllist=baseDao.queryListObjectAll(controlHql);
			//去除list中手工排序的作品集
			if(list!=null&&list.size()>0){								
				 Iterator<VLpGalaryAppShow> sourceIt=list.iterator();
			     while(sourceIt.hasNext()){
			    	 VLpGalaryAppShow tmpSharedBoardSmsWrapper=sourceIt.next();
			    	 if(tmpSharedBoardSmsWrapper.getIndexControl()==1){
			    		 sourceIt.remove(); 
			    	 }
			    }
			}			
			 //把[begin,end]的手工数据插入到list中
			if(controllist!=null&&controllist.size()>0){								
				for(VLpGalaryAppShow gallery:controllist){
					//手工
					int index=gallery.getGalaryIndex();
					if(index-1<list.size()){
					list.add(index-1, gallery);
					}else{
						list.add(gallery);
						
					}
				}		
			}
			List<VLpGalaryAppShow> newList = new ArrayList<VLpGalaryAppShow>();
			for(int i=0;i<list.size();i++){
				VLpGalaryAppShow  galary= list.get(i);
				if(galary.getGalaryCover().startsWith("/upload")){
					galary.setGalaryCover(serverName+galary.getGalaryCover());
				}
				int likeNum = baseDao.getCount("from LpLike where lpGalary.galaryId='"+galary.getGalaryId()+"' and lpUser.userId='"+userId+"' and likeStatus=0 and likeType=0");
				if(likeNum >0){
					galary.setIsLike(0); //赞过
				}else{
					galary.setIsLike(1); //没有赞过
				}
				newList.add(galary);
			}
			return newList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//查作品集的评论数
    public int getCommentCount(String gallaryid){
    	 String hql = "from LpComment lc where lc.lpGalary.galaryId='"+gallaryid
 				+"' and lc.lpUser.userId=(select lu.userId from LpUser lu " +
 				"where lc.lpUser.userId=lu.userId and lu.userStatus=0) order by lc.createTime desc";
     int countNumber= baseDao.getCount(hql);     
     return countNumber;
   }
    /**
     * 
    
     * @Description:插入专题
    
     * @param list
    
     * void
    
     * @exception:
    
     * @author: lxd
    
     * @time:2015-3-20 下午6:55:45
     */
    public void insertSubject(List<VLpGalaryAppShow> list,String serverName,String pageStr){
    	
    	int page = 1;
    	
    	if(StringUtils.isNotBlank(pageStr)){
    		
    		page = Integer.parseInt(pageStr);
    		
    	}
    	
    	
    	//该页的起始位置
		int begin =(page-1)*10+1;
		//该页的结束位置
		int end=page*10;
		
		String controlHql="from Subject where  index_location between " + begin + " and " + end + " and subject_status=0 order by index_location";
		List<Subject> subjectList=new ArrayList<Subject>();
		subjectList=baseDao.queryListObjectAll(controlHql);
		 //把[begin,end]的手工数据插入到list中
		if(subjectList!=null&&subjectList.size()>0){								
			for(Subject subject:subjectList){
				//手工
				String subjectId=subject.getSubject_id();
				int index=subject.getIndex_location();
				VLpGalaryAppShow search =new VLpGalaryAppShow();
				search.setSubjectId(subjectId);
				search.setSubjectCover(serverName + subject.getSubject_img().replace(LaipaiConstants.UPLOAD_ABSOLUTE_IMG+"/lpSubjectImg", LaipaiConstants.UPLOAD_VIRTUAL_IMG+"/lpSubjectImg"));
				String hql = "from VLpSubjectDetailApp where subjectId='"+subjectId+"'";
				int gallaryNum= baseDao.getCount(hql);
				search.setGallaryNum(gallaryNum);
				search.setDiscoverName(subject.getSubject_name());
				search.setIsSubject(1);
				if(index-1<list.size()){
				list.add(index-1, search);
				}else{
					
					list.add(search);
				}
			}		
		}
    	
    	
    }
    /**
     * 
    
     * @Description:计算专题的总赞数
    
     * @param subjectId
     * @return
    
     * String
    
     * @exception:
    
     * @author: lxd
    
     * @time:2015-3-20 下午7:39:15
     */
   
    public String getlikeNumber(String subjectId){
		//重新计算点赞数
		String likeNum = "0";
		try {
			String sql  = "select ifnull(sum(a.like_number),0) like_number from ( "
						 +" SELECT sum(ifnull(lge.like_number,0))  like_number "  
						 +" FROM  lp_galary lg "
						 +" left join  lp_galary_extend lge on lge.galary_id = lg.galary_id "  
						+"  where lg.control_source = 1 "
						+" and lg.galary_status=0 "
						+" and lg.status=0 "
						+"  and lge.extend_id is not null "
						+"  and lg.galary_id in (SELECT lp_subject_detail.gallery_id FROM lp_subject_detail where lp_subject_detail.subject_id ='"+subjectId+"') "
						+" union all "
						+"  SELECT sum(ifnull(lg.like_number,0))  like_number "
						+"  FROM  lp_galary lg    "
						+"  where  lg.galary_status=0 "
						+" and lg.status=0 "
						+" and  lg.control_source = 0 "
						+"  and  lg.galary_id in (SELECT lp_subject_detail.gallery_id FROM lp_subject_detail where lp_subject_detail.subject_id ='"+subjectId+"') "
						+" ) a ";
			List LikeList = baseDao.querySqlObject(sql);
			if(LikeList!=null && !LikeList.isEmpty()){
				Map map = (Map) LikeList.get(0);
				int num = ((BigDecimal) map.get("like_number")).intValue();
				if(num >999){
					likeNum = (int)Math.floor(num/1000)+"K";
				}else{
					likeNum = num+"";
				}
			}
    	
		}catch (Exception e) {
			e.printStackTrace();
		}
		return likeNum;
    }
    
    
    public void inserLog(String gallaryId,String userId,String machineId){
    	
    	LpGalaryShowlog showLog=new LpGalaryShowlog();
    	showLog.setDetailId(gallaryId);
    	if(StringUtils.isBlank(userId)){
    		showLog.setMachineId(machineId);
    		
    	}
    	showLog.setShowTime(new Timestamp(new java.util.Date().getTime()));
    	baseDao.save(showLog);
     }
    	
    } 


